package bookstore.service;

import bookstore.handle.Msg;
import bookstore.mapper.AddressMapper;
import bookstore.mapper.BookMapper;
import bookstore.mapper.OrderMapper;
import bookstore.model.Book;
import bookstore.model.Order;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    BookMapper bookMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    ShopCartService shopCartService;

    //得到用户所有订单
    public Object allOrder(Integer userId) {
        if (null == userId) {
            return new Msg(0, "请传入你的用户id");
        }
        List<Order> orderList = orderMapper.allOrder(userId);
        Collections.sort(orderList, new Comparator<Order>() {
            @Override
            public int compare(Order o1, Order o2) {
                return o2.getId()-o1.getId();
            }
        });
        return new Msg(200, "", orderList);
    }

    //删除一个或者多个订单
    public Object delOrder(List<Integer> listOrderId) {
        if (null == listOrderId || listOrderId.size() == 0) {
            return new Msg(0, "参数不合法");
        }
        int i = orderMapper.deleteBatchIds(listOrderId);
        return new Msg(200, "删除订单成功", i);
    }

    //增加一个或多个订单
    public Object addOrder(Integer userId, String orderStr) {
        JSONArray jsonArray = JSONArray.fromObject(orderStr);
        List orderList = new ArrayList();
        List bookIdList = new ArrayList();
        for (int i = 0; i < jsonArray.size(); i++) {
            Long orderNum = System.currentTimeMillis();
            JSONObject jsonOne = jsonArray.getJSONObject(i);
            Object bookIdObject = jsonOne.get("book_id");
            Object addressIdObject = jsonOne.get("address_id");
            Object countObject = jsonOne.get("count");
            if (
                    null == bookIdObject ||
                            !(bookIdObject instanceof Integer) ||
                            null == addressIdObject ||
                            !(addressIdObject instanceof Integer) ||
                            null == countObject ||
                            !(countObject instanceof Integer)
            ) {
                return new Msg(0, "参数不合法");
            } else {
                Integer count = (Integer) countObject;
                if (count <= 0) {
                    return new Msg(0, "参数不合法");
                }
                Integer bookId = (Integer) bookIdObject;
                Integer addressId = (Integer) addressIdObject;
                if (null == bookMapper.selectById(bookId)) {
                    return new Msg(0, "书籍不存在");
                }
                if (null == addressMapper.selectById(addressId)) {
                    return new Msg(0, "地址不存在");
                }
                Order order = new Order();
                order.setBookId(bookId);
                bookIdList.add(bookId);
                order.setAddressId(addressId);
                order.setCount(count);
                order.setOrderNum(String.valueOf(orderNum));
                order.setUserId(userId);
                Book book = bookMapper.selectById(bookId);
                Double price = Double.parseDouble(String.valueOf(Integer.parseInt(book.getPrice()) * count));
                order.setPrice(price);
                orderList.add(orderMapper.insert(order));
            }
        }
        shopCartService.delOneCart(userId, bookIdList.toString());
        return new Msg(200, "添加订单成功", orderList);
    }
}
